<%--
  Created by IntelliJ IDEA.
  User: 86137
  Date: 2022/3/20
  Time: 21:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>restful</title>
    <script src="/js/jquery-1.11.1.js"></script>
</head>
<body>
    <form id="myForm" action="" method="post">
        球队id：<input type="text" name="id" id="teamId"/> <br>
        球队名称：<input type="text" name="name"/><br>
        球队地址：<input type="text" name="location"><br>
        <button type="button" id="btnGetAll">查询所有球队的信息</button>
        <button type="button" id="btnGetOne">查询某一个球队的信息</button>
        <button type="button" id="btnPost">添加一个新球队信息</button>
        <button type="button" id="btnPUT">修改一个新球队信息</button>
        <button type="button" id="btnDEL">删除一个球队信息</button>
    </form>
    <p id="showResult"></p>
</body>
</html>
<script>

    //修改PUT修改球队的信息
    $(function (){
       $("#btnPUT").click(function (){
          $.ajax({
             type: "POST",
             url: "/team/"+$("#teamId").val(),
              data:$("#myForm").serialize()+"&_method=PUT",
              dateType: "json",
              success: function (msg){
                 alert(msg);
              }
          });
       });
    });

    //删除球队信息
    $(function(){
       $("#btnDEL").click(function(){
            $.ajax({
                type: "POST",
                url: "/team/"+$("#teamId").val(),
                data: "&_method=DELETE",
                success: function(msg){
                    alert("删除成功");
                }
            });
       });
    });


    //使用POST添加一条新的球队信息
    $(function (){
       $("#btnPost").click(function (){
          $.ajax({
              type: "POST",
              url: "/team",
              data: $("#myForm").serialize(),
              dataType:"json",
              success: function(msg){
                  $("#showResult").html(msg);
              }
          });
       });
    });

    //根据id查询某一个球队的信息    /team/1001
    $(function(){
       $("#btnGetOne").click(function(){
         $.ajax({
             type: "GET",
             url: "/team/"+$("#teamId").val(),
             data: "",
             dataType: "json",
             success: function (obj){
                 if (obj==""){
                     $("#showResult").html("没有满足条件的数据！");
                 }else{
                     $("#showResult").html(obj.id+","+obj.name+","+obj.location);
                 }
             }
         });
       });
    });


    //查询所有球队的信息
    $("#btnGetAll").click(function (){
        $.ajax({
            type:"GET",
            url: "/teams",
            data: "",
            dataType: "json",
            success: function (list){
                var str = "";
                for (var i=0;i<list.length;i++){
                    str += list[i].id+"-----------"+list[i].name+"------------"+list[i].location+"<br/>"
                }
                $("#showResult").html(str);
            }
        });
    });
</script>